package com.atguigu.exam.controller;


import com.atguigu.exam.common.Result;
import com.atguigu.exam.entity.Notice;
import com.atguigu.exam.service.NoticeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "公告管理")
@RestController
@RequestMapping("/api/notices")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    @PutMapping("/update")
    @Operation(summary = "更新公告信息")
    public Result update(@RequestBody Notice notice) {
        boolean update = noticeService.updateById(notice);
        if (update) {
            return Result.success("更新成功");
        } else {
            return Result.error("更新失败");
        }
    }

    @Operation(summary = "切换公告状态")
    @PutMapping("toggle/{id}")
    public Result toggle(@PathVariable Long id, @RequestParam Boolean isActive) {
        LambdaUpdateWrapper<Notice> noticeLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        noticeLambdaUpdateWrapper.set(Notice::getIsActive, isActive).eq(Notice::getId, id);
        boolean update = noticeService.update(noticeLambdaUpdateWrapper);
        if (update) {
            return Result.success("切换成功");
        } else {
            return Result.error("切换失败");
        }
    }

    @Operation(summary = "发布新公告")
    @PostMapping("/add")
    public Result add(@RequestBody Notice notice) {
        boolean save = noticeService.save(notice);
        return save ? Result.success("添加成功") : Result.error("添加失败");
    }

    @Operation(summary = "根据ID获取公告")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id){
        Notice byId = noticeService.getById(id);
        return Result.success(byId, "获取成功");
    }
   @Operation(summary = "获取所有公告")
   @GetMapping("/list")
    public Result<List<Notice>> findAll(){
       List<Notice> list = noticeService.list();
       return Result.success(list,"获取所有公告成功");
   }
   @Operation(summary = "获取最新公告")
   @GetMapping("/latest")
    public Result<List<Notice>> latest(@RequestParam Integer limit){
       return  noticeService.getActiveNotices(limit);
   }
    @Operation(summary = "获取启用的公告")
    @GetMapping("/active")
    public Result findActive(){
        LambdaQueryWrapper<Notice> noticeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        noticeLambdaQueryWrapper.eq(Notice::getIsActive, true);
        List<Notice> list = noticeService.list(noticeLambdaQueryWrapper);
        return Result.success(list, "获取所有启用的公告成功");
    }
    @Operation(summary = "删除公告")
    @DeleteMapping("/delete/{id}")
    public Result deleteById(@PathVariable long id){
        boolean b = noticeService.removeById(id);
        return b ? Result.success("删除成功") : Result.error("删除失败");
    }
}
